#include "timer.h"
#include "si24r1.h"
void TIMER_Init(void)
{
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3,ENABLE);
	
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = 999; 
    TIM_TimeBaseStructure.TIM_Prescaler = 4799; 
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

//    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

//    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 
//    NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
//    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//    NVIC_Init(&NVIC_InitStructure);

    TIM_Cmd(TIM3, ENABLE);
}
//extern u8 a;
//int timer_ms = 0;
//void TIM3_IRQHandler(void) {
//    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) 
//	{
//        timer_ms++;
//		if(timer_ms == 10)
//		{
//			a++;
//			timer_ms = 0;
//		}
//		
//        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
//    }
//}

void Delay_100ms(uint32_t ms) 
{
    uint32_t i;
    TIM_Cmd(TIM3, ENABLE);
    for (i = 0; i < ms; i++) 
	{
        while (TIM_GetFlagStatus(TIM3, TIM_FLAG_Update) == RESET);
        TIM_ClearFlag(TIM3, TIM_FLAG_Update);
    }
    TIM_Cmd(TIM3, DISABLE);
}
